home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13078 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: ccshst05.cs.uoguelph.ca!ccshst01!thay
  2. From: thay@uoguelph.ca (Toby K Hay)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: segmentation fault
  5. Date: 4 Apr 1996 12:33:57 GMT
  6. Organization: University of Guelph
  7. Message-ID: <4k0fjl$ir@ccshst05.cs.uoguelph.ca>
  8. References: <31636B02.55C6@cts.com>
  9. NNTP-Posting-Host: ccshst01.cs.uoguelph.ca
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Frank Kragh (kragh@cts.com) wrote:
  13. : When running a program, (which didn't yield any errors during compiling), 
  14. : I get a "Segmentation fault".  Anybody know what this is and/or what 
  15. : might be wrong with my program?
  16.  
  17.  I posted this same question here recently and received plenty of 
  18. good advice which I'll summarize very briefly.  A segmentation fault 
  19. while running a program usually (always?) means that your program is 
  20. accessing memory that wasn't allocated to it.  It doesn't happen on a DOS 
  21. machine becuase the OS doesn't do that check - the program can write 
  22. wherever it likes, maybe overwriting something vital and crashing the 
  23. machine, or maybe not.  On UNIX the OS prevents it and halts the program 
  24. with a segmentation fault.  
  25.  Most people who replied to my question suggested:
  26. 1) after a call to malloc() check that the returned pointer is not NULL 
  27. so that the program doesn't ever try to access memory via a null pointer.
  28. 2) check that all calls to free() had a valid pointer - no trying to 
  29. free() a pointer without a previous call to malloc() or that had already 
  30. been free()d.
  31. 3) check that the program wasn't reading or writing arrays beyond their 
  32. declared bounds (e.g. trying to access array elements RA[-1] or RA[7] on 
  33. an array declared as int RA[7] whose elements will be numbered 0 to 6).  
  34. (This was the bug in my program.)
  35. Toby Hay   thay@uoguelph.ca
  36.  
  37.  
  38.